import { mergeOptions, convertColorFormat, jsonToVec3 } from "../core/common.js";
import global from "../core/global.js";

let Polygon3D = {
  add(options, cb) {
    let defaults = {
      geojson: null,
      points: [],
      style: {
        mode: "RelativeToTerrain",
        altitude: 3,
        lineColor: "",
        color: "#00FF00",
        imgUrl: "",
        alpha: 0.99,
        fillAlpha: 0.99,
        waterShader: false,
        offsetX: 0,
        offsetY: 0,
        scale: 1,
        dash: false,
        segment: 12,
        repeat: 0,
        borderWidth: 0,
      },
    };
    options = mergeOptions(defaults, options);

    if (!options.geojson && options.points.length < 3) {
      throw new TypeError("请传入geojson面数据或大于3个点的points");
    }

    let polygon = null;
    if (options.geojson) {
      polygon = this.jsonRender(options);
    } else {
      let points = jsonToVec3(options.points);
      polygon = this.render(points, options.style);
    }

    if (options.id) polygon.tag = options.id
    let id = global.Polygon3DStore.add(polygon, options.id);
    cb && cb(id);
    return id;
  },
  jsonRender(options) {
    let geojson = options.geojson;
    let entity = new SSmap.Entity();
    geojson.features.forEach((feature) => {
      let coordinates = feature.geometry.coordinates;
      let points =
        feature.geometry.type == "MultiPolygon"
          ? coordinates[0][0]
          : coordinates[0];
      points = jsonToVec3(points);

      let polygon = Polygon3D.render(points, options.style);
      polygon.parent = entity;
    });
    GlobalViewer.scene.addEntity(entity);
    return entity;
  },
  render(
    points,
    {
      borderWidth,
      lineColor,
      color,
      alpha,
      fillAlpha,
      imgUrl,
      mode,
      dash,
      waterShader,
      altitude,
      bottomHeight,
      limitHeight,
      repeat,
    }
  ) {
    color = convertColorFormat(color);
    lineColor = lineColor ? convertColorFormat(lineColor) : color;
    let polygon = new SSmap.Polygon3D();
    polygon.dash = dash;
    polygon.color = SSmap.Color.fromRgb(
      lineColor.r,
      lineColor.g,
      lineColor.b,
      255
    );
    if (borderWidth) {
      polygon.setWidth(borderWidth);
    }
    polygon.alpha = alpha;
    polygon.setFillColor(
      SSmap.Color.fromRgb(color.r, color.g, color.b, 255)
    );
    polygon.fillAlpha = fillAlpha;
    polygon.waterShader = waterShader;
    if (imgUrl) {
      polygon.fillMode = SSmap.FillMode.FillImage;
      polygon.image = imgUrl;
      polygon.alpha = 0;
      polygon.setRepeat(repeat);
    }
    if (altitude) {
      polygon.setAltitude(altitude);
    }
    if (bottomHeight) {
      polygon.setHeight(bottomHeight);
    }
    if (limitHeight) {
      polygon.setLimitHeight(limitHeight);
    }

    points.forEach((point) => {
      polygon.addPoint(point);
    });
    polygon.setAltitudeMethod(SSmap.AltitudeMethod[mode]);
    polygon.addProperty("name", "polygon");
    polygon.draw();
    polygon.end();
    return polygon;
  },
  remove(id) {
    global.Polygon3DStore.has(id) && global.Polygon3DStore.remove(id);
  },
  changeProperty(id, property) {
    let polygon = global.Polygon3DStore.get(id);
    if (polygon) {
      for (const key in property) {
        if (Object.hasOwnProperty.call(property, key)) {
          const value = property[key];
          polygon[key] = value;
        }
      }
    }
  },
  returnPolygon3D(id) {
    let polygon = global.Polygon3DStore.get(id);
    return polygon
  }
};

export default Polygon3D;
